package com.blue.user.mq.listeners;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.context.annotation.Configuration;

/**
 * 监听器如何生效：在QueueMappingMonitorEnum枚举中定义好相关队列，以及对应的监听器
 *
 * @author liulei
 * @version 1.0
 */
@Slf4j
@Configuration
public class BusinessListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            log.info("============> Thread1:[{}] 第一个Mq的Test队列监听接收到消息:[{}] ", Thread.currentThread().getName(), new String(message.getBody()));
            log.info("====>connection:[{}]", channel.getConnection());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // 发生异常此处需要捕获到
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
